package cn.juque.datapipeline.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.juque.common.base.BaseRequestVO;
import cn.juque.common.base.BaseResponseVO;
import cn.juque.common.base.PageInfo;
import cn.juque.common.constants.MessageEnum;
import cn.juque.datapipeline.bo.group.TaskGroupInfoAddBO;
import cn.juque.datapipeline.bo.group.TaskGroupInfoUpdateBO;
import cn.juque.datapipeline.domain.TaskGroupRelationDO;
import cn.juque.datapipeline.entity.TaskGroupInfo;
import cn.juque.datapipeline.service.ITaskGroupInfoService;
import cn.juque.datapipeline.vo.taskgroupinfo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>TaskGroupInfoController</li>
 * </ul>
 * @date 2023-04-02 14:33:19
 **/
@Slf4j
@Controller
@RequestMapping("/dpp/group")
public class TaskGroupInfoController extends BaseController {

    @Resource
    private ITaskGroupInfoService taskGroupInfoService;

    /**
     * 查询
     *
     * @param request BaseRequestVO
     * @return BaseResponseVO
     */
    @PostMapping(value = "/list")
    @ResponseBody
    public BaseResponseVO<List<TaskGroupInfoListVO>> list(@RequestBody BaseRequestVO<TaskGroupInfoSearchVO> request) {
        TaskGroupInfoSearchVO searchVO = super.validatorUtil.checkNotNull(request);
        PageInfo pageInfo = request.getPageInfo();
        super.validatorUtil.checkPageInfo(pageInfo);
        TaskGroupInfo taskGroupInfo = new TaskGroupInfo();
        BeanUtil.copyProperties(searchVO, taskGroupInfo);
        List<TaskGroupInfo> list = this.taskGroupInfoService.listGroupInfoWithPage(pageInfo, taskGroupInfo);
        List<TaskGroupInfoListVO> voList = BeanUtil.copyToList(list, TaskGroupInfoListVO.class);
        return new BaseResponseVO<>(MessageEnum.OK, voList, pageInfo);
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return BaseResponseVO
     */
    @GetMapping(value = "/detail")
    @ResponseBody
    public BaseResponseVO<TaskGroupInfoDetailVO> detail(String id) {
        Assert.notEmpty(id);
        TaskGroupInfo detail = this.taskGroupInfoService.detail(id);
        TaskGroupInfoDetailVO detailVO = new TaskGroupInfoDetailVO();
        BeanUtil.copyProperties(detail, detailVO);
        List<TaskGroupRelationDO> relationList = this.taskGroupInfoService.listTaskRelation(id);
        List<TaskGroupRelationListVO> relationVoList = BeanUtil.copyToList(relationList, TaskGroupRelationListVO.class);
        detailVO.setTaskList(relationVoList);
        return new BaseResponseVO<>(MessageEnum.OK, detailVO);
    }

    /**
     * 新增
     *
     * @param request BaseRequestVO
     * @return BaseResponseVO
     */
    @PostMapping(value = "/add")
    @ResponseBody
    public BaseResponseVO<Boolean> add(@RequestBody BaseRequestVO<TaskGroupInfoAddVO> request) {
        super.validatorUtil.fastValidate(request);
        TaskGroupInfoAddVO addVO = request.getParam();
        TaskGroupInfoAddBO addBO = new TaskGroupInfoAddBO();
        BeanUtil.copyProperties(addVO, addBO);
        this.taskGroupInfoService.saveGroupInfo(addBO);
        return new BaseResponseVO<>(MessageEnum.OK, true);
    }

    /**
     * 更新
     *
     * @param request BaseRequestVO
     * @return BaseResponseVO
     */
    @PostMapping(value = "/edit")
    @ResponseBody
    public BaseResponseVO<Boolean> edit(@RequestBody BaseRequestVO<TaskGroupInfoUpdateVO> request) {
        super.validatorUtil.fastValidate(request);
        TaskGroupInfoUpdateVO updateVO = request.getParam();
        TaskGroupInfoUpdateBO updateBO = new TaskGroupInfoUpdateBO();
        BeanUtil.copyProperties(updateVO, updateBO);
        this.taskGroupInfoService.updateGroupInfo(updateBO);
        return new BaseResponseVO<>(MessageEnum.OK, true);
    }

    /**
     * 禁用
     *
     * @param id 主键
     * @return BaseResponseVO
     */
    @GetMapping(value = "/triggerEnable")
    @ResponseBody
    public BaseResponseVO<Boolean> triggerEnable(String id) {
        Assert.notEmpty(id);
        this.taskGroupInfoService.triggerEnable(id);
        return new BaseResponseVO<>(MessageEnum.OK, true);
    }

    /**
     * 禁用
     *
     * @param id 主键
     * @return BaseResponseVO
     */
    @GetMapping(value = "/startOnce")
    @ResponseBody
    public BaseResponseVO<Boolean> startOnce(String id) {
        Assert.notEmpty(id);
        this.taskGroupInfoService.startOnce(id);
        return new BaseResponseVO<>(MessageEnum.OK, true);
    }

    /**
     * 删除
     *
     * @param request BaseRequestVO
     * @return BaseResponseVO
     */
    @PostMapping(value = "/delete")
    @ResponseBody
    public BaseResponseVO<Boolean> delete(@RequestBody BaseRequestVO<TaskGroupInfoDeleteVO> request) {
        super.validatorUtil.fastValidate(request);
        List<String> idList = request.getParam().getIdList();
        this.taskGroupInfoService.deleteGroupInfo(idList);
        return new BaseResponseVO<>(MessageEnum.OK, true);
    }
}
